<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="信息描述">
        <el-tag type="info">课程分类模板</el-tag>
        <el-tag>
          <i class="el-icon-download"/>
          <a :href="defaultExcelTemplate">点击下载模版</a>
        </el-tag>

      </el-form-item>

      <el-form-item label="选择Excel">
        <el-upload
          ref="upload"
          :on-exceed="fileUploadExceed"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false"
          :limit="1"
          name="subjects"
          class="upload-demo"
          action="http://localhost:8110/admin/edu/subject/import">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
          <div slot="tip" class="el-upload__tip">只能上传一个xsls文件</div>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      fileList: [], // 上传文件集合
      // importUrl: 'http://localhost:8110/admin/edu/subject/import',
      // defaultExcelTemplate: process.env.OSS_PATH + '/excel/课程分类列表模板.xls', // 默认Excel模板
      defaultExcelTemplate: 'https://sh201130-guli-file.oss-cn-shanghai.aliyuncs.com/excel/课程分类列表模板.xlsx'
    }
  },
  methods: {
    // 上传多于一个文件时
    fileUploadExceed() {
      this.$message.warning('只能选取一个文件')
    },
    submitUpload() {
      this.$refs.upload.submit() // 提交上传请求
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    fileUploadSuccess(response, file, fileList) {
      // console.log(response)

      if (response.code === 20000) {
        this.$message.success('文件上传成功')
      } else {
        this.$message.error('文件上传失败')
      }
      this.fileList = []
    },

    fileUploadError(response, file, fileList) {
      this.$message.error('文件上传失败')
      this.fileList = [] // 清空文件列表
    }
  }
}
</script>
